package com.jbp.service.impl;

import com.jbp.model.Room;
import com.jbp.model.RoomUserWheat;
import com.jbp.service.RoomService;
import com.jbp.service.RoomUserWheatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import com.jbp.dao.RoomWheatDao;
import com.jbp.model.RoomWheat;
import com.jbp.service.RoomWheatService;


@Service
public class RoomWheatServiceImpl extends ServiceImpl<RoomWheatDao, RoomWheat> implements RoomWheatService {

    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomUserWheatService roomUserWheatService;
    @Autowired
    private RoomWheatService roomWheatService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoomWheat> page = this.page(
                new Query<RoomWheat>().getPage(params),
                new QueryWrapper<RoomWheat>()
        );

        return new PageUtils(page);
    }

    //麦位心动值增加
    @Override
    public void addCharm(Integer userId, Integer roomId, int charm) {
        Room room = roomService.getById(roomId);
        //判断心动值开关是否开启
        boolean flag = room.getOnCharm()==1?true:false;
        //开启 麦位心动值增加
        if (flag){
            int place = -1;
            if (room.getUserId().equals(userId)) {
                //房主，直接添加0号麦
                place = 0;
            } else {
                RoomUserWheat userWheat = roomUserWheatService.getOne(new QueryWrapper<RoomUserWheat>().eq("user_id", userId)
                        .eq("room_id", room.getId()));
                if (userWheat != null) {
                    place = userWheat.getRoomWheat();
                }
            }

            if (place != -1) {
                RoomWheat roomWheat = roomWheatService.getOne(new QueryWrapper<RoomWheat>().eq("room_id", roomId)
                        .eq("place", place));
                if (roomWheat != null) {
                    roomWheat.setCharm(roomWheat.getCharm() + charm);
                    roomWheatService.updateById(roomWheat);
                }
            }
        }
    }


}
